//
//  ZCameraICardController.m
//  danpin
//
//  Created by chuangjia on 27/11/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "ZCameraICardController.h"
#import "ZCameraPhotoEdit.h"
#import "ZCameraSlideView.h"
#import "ZCameraSlideUpView.h"
#import "ZCameraSlideDownView.h"
#import "viewconfig.h"


@interface ZCameraICardController ()

@property (strong, nonatomic) UIImageView *cardfView;
@property (strong, nonatomic) UIImageView *cardbView;
@property (strong, nonatomic) UIView *captureView;
@property (strong, nonatomic) UIView *bottomView;

@property (strong, nonatomic) UIButton *flashButton;
@property (strong, nonatomic) UIButton *shotButton;

@property (strong, nonatomic) ZCamera *camera;
@property (nonatomic) CGFloat beginPinchGestureScale;
@property (nonatomic) CGFloat effectiveScale;
@property (nonatomic) BOOL wasLoaded;

- (IBAction)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer;
- (IBAction)handleTapGesture:(UITapGestureRecognizer *)recognizer;

- (void)zoomWithRecognizer:(UIPinchGestureRecognizer *)recognizer;

@end



@implementation ZCameraICardController
@synthesize isFront = _isFront;
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor=CameraRGB(0x1e1e1e);
    
    if (!_camera){
        _camera = [ZCamera cameraWithFlashButton:_flashButton];
    }
    _effectiveScale = 1.;
    
    [self.view addSubview:self.captureView];
    if (_isFront) {
        [self.view addSubview:self.cardfView];
        self.cardfView.center=CGPointMake(self.view.center.x, self.view.center.y-50);
    }else{
        [self.view addSubview:self.cardbView];
         self.cardbView.center=CGPointMake(self.view.center.x, self.view.center.y-50);
    }
   
    [self.view addSubview:self.bottomView];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    
    _flashButton.enabled = YES;
    _shotButton.enabled = YES;
    [_camera startRunning];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    if (_wasLoaded == NO) {
        _wasLoaded = YES;
        [_camera insertSublayerWithCaptureView:self.captureView atRootView:self.view];
    }
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
     [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
    [_camera stopRunning];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    NSLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)dealloc{
    self.captureView=nil;
    self.bottomView=nil;
    [self.camera stopRunning];
    self.camera=nil;
    
    NSLog(@"%s", __PRETTY_FUNCTION__);
}
- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (BOOL)shouldAutorotate
{
    return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

#pragma mark -
#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
        _beginPinchGestureScale = _effectiveScale;
    }
    
    return YES;
}


- (IBAction)flashTapped
{
    [_camera changeFlashModeWithButton:_flashButton];
}

- (IBAction)handleTapGesture:(UITapGestureRecognizer *)recognizer
{
    CGPoint touchPoint = [recognizer locationInView:self.captureView];
    [_camera focusView:self.captureView inTouchPoint:touchPoint];
    
}

#pragma mark -
#pragma mark - Private methods

//
-(UIImageView*)cardbView
{
    if (!_cardbView) {
        UIImage *bimage = [UIImage imageNamed:@"icardb"];
        if (IS_IPHONE_6P) {
            _cardbView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, bimage.size.width/5*4, bimage.size.height/5*4)];
        }else{
            _cardbView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, bimage.size.width/5*3 , bimage.size.height/5*3)];
        }
        _cardbView.image = bimage;
    }
    return _cardbView;
}
-(UIImageView*)cardfView
{
    if (!_cardfView) {
        UIImage *fimage = [UIImage imageNamed:@"icardf"];
        if (IS_IPHONE_6P){
            _cardfView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, fimage.size.width/5*4, fimage.size.height/5*4)];
        }else{
           _cardfView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, fimage.size.width/5*3, fimage.size.height/5*3)];
        }
        _cardfView.image = fimage;
    }
    return _cardfView;
}
-(UIView*)captureView
{
    if (!_captureView) {
        UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
        self.captureView =[[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height -self.cardfView.frame.size.height , self.cardfView.frame.size.width,  self.cardfView.frame.size.height)];
        self.captureView.backgroundColor = [UIColor clearColor];
        self.captureView.userInteractionEnabled =YES;
        [self.captureView addGestureRecognizer:tapGesture];
        self.captureView.center =CGPointMake(self.view.center.x, self.view.center.y-50);
        tapGesture=nil;
      
    }
    return _captureView;
}

-(UIView*)bottomView
{
    if (!_bottomView) {
        self.bottomView =[[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 100, self.view.frame.size.width,100)];
        self.bottomView.backgroundColor = CameraRGB(0x1e1e1e);
        
        //底部工具兰
        UIView *toolBarView =[[UIView alloc] initWithFrame:CGRectMake(0,0, self.bottomView.frame.size.width,100)];
        toolBarView.backgroundColor= CameraRGB(0x1e1e1e);
        //
        _shotButton = [[UIButton alloc] initWithFrame:CGRectMake((self.bottomView.frame.size.width -80)/2, 10, 80,80)];
        _shotButton.tag=9;
        [_shotButton setImage:[UIImage imageNamed:@"shot.png"] forState:UIControlStateNormal];
        [_shotButton addTarget:self action:@selector(toolButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [toolBarView addSubview:_shotButton];
        
        //
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        backBtn.tag=10;
        UIImage *backImage = [UIImage imageNamed:@"back1.png"];
        [backBtn setImage: backImage forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(toolButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [backBtn setFrame:CGRectMake(10, 25, 50, 50)];
        [toolBarView addSubview:backBtn];
        //
        
        _flashButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _flashButton.tag =11;
        [_flashButton setBackgroundImage:[UIImage imageNamed:@"shanguangdeng.png"] forState:UIControlStateNormal];
        [_flashButton addTarget:self action:@selector(toolButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [_flashButton setFrame:CGRectMake(self.bottomView.frame.size.width-60, 25, 50, 50)];
        [toolBarView addSubview:_flashButton];
        
        [self.bottomView addSubview:toolBarView];
    }
    return _bottomView;
}
-(void)toolButtonPressed:(id)sender
{
    UIButton *btn =(UIButton *)sender;
    switch (btn.tag) {
        case 9:
        {
            _shotButton.enabled = NO;
            if (_camera) {
                [_camera takePhotoWithCaptureViewForCard:self.captureView effectiveScale:_effectiveScale videoOrientation:AVCaptureVideoOrientationLandscapeRight completion:^(UIImage *photo) {
                    _shotButton.enabled = YES;
                    if ([_delegate respondsToSelector:@selector(cameraDidTakePhoto:)]) {
                        [_delegate cameraDidTakePhoto:photo];
                    }
                }];
            }
        }
            break;
        case 10:
        {
            if (self.navigationController) {
                if (self.navigationController.viewControllers.count == 1) {
                    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                } else {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            } else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
            break;
        case 11:
        {
            if (_camera) {
                [_camera changeFlashModeWithButton:_flashButton];
            }
        }
            break;
       
        default:
            break;
    }
}
@end
